
$(function() {
	Tip.emp('负责人');
	
	$("select[id^='invoice_s']:visible").multiselect().multiselect("uncheckAll");
	
	$("input[id^='amount']:visible").css("width", 85);
	
	Tip.supplier();
});

var PaymentCustomer = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				
				body.iscustomer = 1;
				body.supplier = form.supplier.value;
				body.supplier_x = Supplier.supplier_x(form);
								
				body.type = form.type.value;
				body.name = form.name.value;
				body.amount = form.amount.value;
				body.currency = form.currency.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.invoice = form.invoice.value;
				body.urgent = form.urgent.value;
				body.payment = form.payment.value;
				body.payment_x = Common.payment_x(form);
				if (!body.payment_x) {
					return false;
				}
				
				body.voucher = form.voucher.value;
				body.voucher_x = form.voucher_x.value;
				
				body.content = form.content.value;
								
				body.emp = form.emp.value;

				$(this).dialog("close");

				$.post('finance.php?do=PaymentCustomer.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		$.cookie('status_', status);

		p2 = query(p2);
		$.cookie('p2', p2);
		
		var c1 = 'finance.php?do=PaymentCustomer.edit&id=' + id;
		var c2 = 'finance.php?do=PaymentCustomer.view&id=' + id;
		var c3 = 'finance.php?do=PaymentCustomer.copy&id=' + id;
		
		var c9 = 'finance.php?do=PaymentCustomer.view&print=print&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'PaymentCustomer.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		var a3 = ['复制信息', 'url', 'PaymentCustomer.dialog_copy_', '650x400'];
				
		var a9 = ['打印信息', 'url', 'Common.dialog_view_', '650x400'];
		
		if (status == 1) {
			var d1 = 'finance.php?do=PaymentCustomer.verify&id=' + id;
			var b1 = ['审核付款', 'url', 'PaymentCustomer.dialog_verify', '650x420'];
			
			var s = d1 + ", " + c1 + ", " + c2 + ", " + c9;

			return popup(e, [b1, a1, a2, a9], s);
		}
		
		if (status == '2') {
			var s = c1 + ", " + c2 + ", " + c3 + ", " + c9;

			return popup(e, [a1, a2, a3, a9], s);
		}		

		var s = c2 + ", " + c3 + ", " + c9;

		return popup(e, [a2, a3, a9], s);
	},
	
	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('finance.php?do=PaymentCustomer.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不提交": function() {
						if (!confirm("确认是否不提交？")) {
							return false;
						}
						
						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.account = form.account.value;
						body.content = form.content.value;
						body.status = 9;
						
						var url = 'finance.php?do=PaymentCustomer.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"提交": function() {
						if (!confirm("确认是否提交付款？")) {
							return false;
						}

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.account = form.account.value;
						body.content = form.content.value;
						
						body.status = 2;
						
						var url = 'finance.php?do=PaymentCustomer.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				
				body.iscustomer = 1;
				body.supplier = form.supplier.value;						
				body.supplier_x = Supplier.supplier_x(form);
				
				body.type = form.type.value;
				body.name = form.name.value;
				body.amount = form.amount.value;
				body.currency = form.currency.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.invoice = form.invoice.value;
				body.urgent = form.urgent.value;
				body.payment = form.payment.value;
				body.payment_x = Common.payment_x(form);
				if (!body.payment_x) {
					return false;
				}
				
				body.voucher = form.voucher.value;
				body.voucher_x = form.voucher_x.value;
				
				body.content = form.content.value;
				
				body.emp = form.emp.value;
				
				$(this).dialog("close");

				var url = 'finance.php?do=PaymentCustomer.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				
				body.iscustomer = 1;
				body.supplier = form.supplier.value;				
				body.supplier_x = Supplier.supplier_x(form);
				
				body.type = form.type.value;
				body.name = form.name.value;
				body.amount = form.amount.value;
				body.currency = form.currency.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.invoice = form.invoice.value;
				body.urgent = form.urgent.value;
				body.payment = form.payment.value;
				body.payment_x = Common.payment_x(form);
				if (!body.payment_x) {
					return false;
				}
				
				body.voucher = form.voucher.value;
				body.voucher_x = form.voucher_x.value;
				
				body.content = form.content.value;
				
				body.emp = form.emp.value;
				
				$(this).dialog("close");

				var url = 'finance.php?do=PaymentCustomer.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=PaymentCustomer.delete' + p2;	
		var c9 = 'finance.php?do=PaymentCustomer.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c9;

		return popup(e, [a1, a9], s);
	}
};
